package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.mob.MobSDK;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;

public class ScoreSubmitActivity extends Activity {

	private TextView tv_totaltime;
	private TextView tv_score;
	private ImageView iv_scoresubmitshowhi;
	private int Score;
	private String totaltime;
	private TextView tv_html_title;
	private ImageView iv_share;
	private TextView tv_word_back;
	private RelativeLayout rl_bg;
	private String account;
	private String LESSON_NAME ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_submit);
		init();
		initData();
		initListener();
		initView();
		
	}

	

	private void init() {
		tv_totaltime = (TextView) findViewById(R.id.tv_totaltime);
		tv_score = (TextView) findViewById(R.id.tv_score);
		tv_html_title= (TextView) findViewById(R.id.tv_html_title);
		iv_share = (ImageView) findViewById(R.id.iv_share);
		iv_scoresubmitshowhi = (ImageView) findViewById(R.id.iv_scoresubmitshowhi);
		tv_html_title= (TextView) findViewById(R.id.tv_html_title);
		tv_word_back= (TextView) findViewById(R.id.tv_word_back);
		rl_bg = (RelativeLayout) findViewById(R.id.rl_bg); 
		rl_bg.setBackgroundResource(R.drawable.scoresubmitshowbgl);
//		Picasso.with(this).load(R.drawable.scoresubmitshowbgs)
//        .into(rl_bg);
	}

	private void initData() {
		account = SPUtils.getString(this, ConfigConstant.USERNAME);
		lESSON_ID = getIntent().getStringExtra("lESSON_ID");
		Log.e("yubo", "scorsubint="+lESSON_ID);
		LESSON_NAME = getIntent().getStringExtra("LESSON_NAME");
		 Score = getIntent().getIntExtra("Score",0);
		int totalTime = getIntent().getIntExtra("totalTime",0);
		uploadjson = getIntent().getStringExtra("Uploadjson");
		Log.e("test",uploadjson);
		exam_type = getIntent().getStringExtra("exam_type");
		totaltime = ""+totalTime;
		
		
		tv_html_title.setText(LESSON_NAME);
		
		
		if(Score<80){
			dialog();
		}
	}
	
	private void dialog() {
		// TODO Auto-generated method stub
		final NormalDialog dialog = new NormalDialog(this);
		dialog.content("建议再做一次！").btnText("取消", "确定")//
				.style(NormalDialog.STYLE_TWO)//
				.titleTextSize(23)//
				.show();
		dialog.setCanceledOnTouchOutside(false);
		dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {

			@Override
			public void onBtnLeftClick() {

				dialog.dismiss();

			}
		});
		
		dialog.setOnBtnRightClickL(new OnBtnRightClickL() {

			@Override
			public void onBtnRightClick() {
				
				if("1".equals(exam_type)){
					Intent intent = new Intent(ScoreSubmitActivity.this,
							NewTestThirdActivity.class);
					intent.putExtra("lESSON_ID", Integer.parseInt(lESSON_ID));
					intent.putExtra("LESSON_NAME", LESSON_NAME);

					startActivity(intent);
					dialog.dismiss();
					finish();
				}else if("2".equals(exam_type)){
					Intent intent = new Intent(ScoreSubmitActivity.this,
							WordFuxiAndroidActivity.class);
					intent.putExtra("lESSON_ID", Integer.parseInt(lESSON_ID));
					intent.putExtra("LESSON_NAME", LESSON_NAME);

					startActivity(intent);
					dialog.dismiss();
					finish();
				}
				}
		});
	}


	private void initListener() {
		tv_word_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		iv_share.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				clickShare2();
			}
		});
	}
	
	private void initView() {
		
		
		tv_score.setText("总分："+Score+"分");
		tv_totaltime.setText("总时间："+totaltime+"秒");
//		tv_html_title.setText(LESSON_NAME);
		if(Score>=80){
			iv_scoresubmitshowhi.setBackgroundResource(R.drawable.scoresubmitshowhi01);
		}else if(Score>=60&&Score<80){
			iv_scoresubmitshowhi.setBackgroundResource(R.drawable.scoresubmitshowhi02);
		}else{
			iv_scoresubmitshowhi.setBackgroundResource(R.drawable.scoresubmitshowhi03);
		}
		
		submitScore();
		
	}
	
	
	  private void submitScore() {
		
		  OkHttpClient mOkHttpClient = new OkHttpClient();
//			mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//		  Builder builder = new FormBody.Builder();
		  FormEncodingBuilder builder = new FormEncodingBuilder();
			
			builder.add("account", account);
			builder.add("lesson_id", ""+lESSON_ID);
			Log.e("yubo", "scorsub="+lESSON_ID);
			builder.add("total_time", totaltime);
			builder.add("score", ""+Score);
			builder.add("exam_type", exam_type);
			builder.add("question_list", uploadjson);
			Request request = new Request.Builder()
					.url(NetConstant.HOST+"App_Class/UnitTest/SaveUnitTestScore.aspx")
					.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(callback);
	}

 

	public void clickShare2() {
//		ShareSDK.initSDK(scoreReportActivity.this);
    	MobSDK.init(ScoreSubmitActivity.this ,"12c24ea0b59b0","aae63565fbd69b1acbfcdd0d5ac0b35c");
		  OnekeyShare oks = new OnekeyShare();
		     //关闭sso授权
		    oks.disableSSOWhenAuthorize(); 
		 	oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
				
				@Override
				public void onShare(Platform platform, ShareParams paramsToShare) {
					String examName="";
					if("2".equals(exam_type)){
						examName="单词检测";
					}else if("1".equals(exam_type)){
						examName="自主检测";
					}
					if(SinaWeibo.NAME.equals(platform.getName())){
						paramsToShare.setTitle(getString(R.string.share));
					     // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
						
						String url="http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type;
						String title=SPUtils.getString(ScoreSubmitActivity.this, "name")+"在"+examName+"环节花费了"+totaltime+"秒获得"+Score+"分,为你点赞！";
						int unicodeJoy = 0x1F44D;
						String emojiString = new String(Character.toChars(unicodeJoy));
						paramsToShare.setTitleUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);
						paramsToShare.setTitle(SPUtils.getString(ScoreSubmitActivity.this, "name")+"在"+examName+"环节花费了"+totaltime+"秒获得"+Score+"分,为你点赞！");
					     // text是分享文本，所有平台都需要这个字段
						paramsToShare.setText(title+"\n\r想展现你的实力吗?来这里比拼一下吧！"+url);
					     // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//					     oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
						paramsToShare.setImageUrl("http://139.129.57.83:8080/Appandroid/applogo.png");
					     // url仅在微信（包括好友和朋友圈）中使用
						paramsToShare.setUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);
					     // comment是我对这条分享的评论，仅在人人网和QQ空间使用
						paramsToShare.setComment("");
					     // site是分享此内容的网站名称，仅在QQ空间使用
						paramsToShare.setSite(getString(R.string.app_name));
					     // siteUrl是分享此内容的网站地址，仅在QQ空间使用
						paramsToShare.setSiteUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);

					  
					}else{
						paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
						paramsToShare.setTitle(getString(R.string.share));
					     // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
						paramsToShare.setTitleUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);
					     paramsToShare.setTitle(SPUtils.getString(ScoreSubmitActivity.this, "name")+"在"+examName+"环节花费了"+totaltime+"秒获得"+Score+"分,为你点赞！");
					     // text是分享文本，所有平台都需要这个字段
					     paramsToShare.setText("想展现你的实力吗?来这里比拼一下吧！");
					     // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//					     oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
					     paramsToShare.setImageUrl("http://139.129.57.83:8080/Appandroid/applogo.png");
					     // url仅在微信（包括好友和朋友圈）中使用
					     paramsToShare.setUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);
					     // comment是我对这条分享的评论，仅在人人网和QQ空间使用
					     paramsToShare.setComment("");
					     // site是分享此内容的网站名称，仅在QQ空间使用
					     paramsToShare.setSite(getString(R.string.app_name));
					     // siteUrl是分享此内容的网站地址，仅在QQ空间使用
					     paramsToShare.setSiteUrl("http://m.moregolden.com:36356/scorePage.html?moduleType=0&userType=0&time="+totaltime+"&mark="+Score+"&num="+SPUtils.getString(ScoreSubmitActivity.this, ConfigConstant.USERNAME)+"&exam_type="+exam_type);

					    
					}
				}
		 	});
		    // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
		     //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
		     // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		    
		 // 启动分享GUI
		     oks.show(this);
	}
	
	
	Callback callback=new Callback() {
		
		

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			// TODO Auto-generated method stub
			
		}
	};
	private String lESSON_ID;
	private String uploadjson;
	private String exam_type;
}
